package com.qianyu.aspect;


import com.qianyu.anno.OperLogAnnotation;
import com.qianyu.entity.bean.OptionLog;
import com.qianyu.entity.bean.User;
import com.qianyu.helper.LoginHelper;
import com.qianyu.service.OptionLogService;
import com.qianyu.util.IpUtil;
import com.qianyu.util.ServletUtils;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;


@Component
@Aspect
@RequiredArgsConstructor
public class OptionLogAspect {

    private final OptionLogService optionLogService;

    @AfterReturning(pointcut = "@annotation(log)", returning = "obj")
    public void afterReturningAdvice(JoinPoint jp, Object obj, OperLogAnnotation log) {
        OptionLog op = new OptionLog();
        op.setMenuName(log.menuName());
        op.setOptionType(log.opeType().getOptionType());
        User loginUser = LoginHelper.getLoginUser();
        op.setUserName(loginUser.getNickname());
        op.setUserId(loginUser.getId());
        op.setParam(Arrays.toString(jp.getArgs()));
        op.setOptionIp(IpUtil.getIpAddr(ServletUtils.getRequest()));
        optionLogService.saveLog(op);
    }

}
